// ActionScript file

package org.sepy.iGallery.core
{
	import com.adobe.webapis.flickr.Photo;
	import com.adobe.webapis.flickr.PhotoSet;
	import com.adobe.crypto.MD5;
	import com.adobe.webapis.flickr.User;
	
	public class Utils
	{
		public static const static_url:String = "http://static.flickr.com/";
		
		public static function getPhotoURI(p:*, size:String = ""):String
		{
			if(p is PhotoSet)
			{
				return Utils.getPhotoSetURI(p, size);
			}
			if(size == "")
			{
				return Utils.static_url + p.server + "/" + p.id + "_" + p.secret + ".jpg";
			} else {
				if(size == "s")
				{
					if(p.thumb)
					{
						return p.thumb;
					} else {
						return Utils.static_url + p.server + "/" + p.id + "_" + p.secret + "_" + size + ".jpg";
					}
				} else {
					return Utils.static_url + p.server + "/" + p.id + "_" + p.secret + "_" + size + ".jpg";
				}
			}
		}
		
		protected static function getPhotoSetURI(p:PhotoSet, size:String=""):String
		{
			if(size == "")
			{
				return Utils.static_url + p.server + "/" + p.primaryPhotoId + "_" + p.secret + ".jpg";
			} else {
				return Utils.static_url + p.server + "/" + p.primaryPhotoId + "_" + p.secret + "_" + size + ".jpg";
			}
		}
		
		/**
		 * Construct the login uri
		 */
		public static function getLoginUri(perm:String="read"):String
		{
			var fromString:String = Config.SECRET+"api_key"+Config.API+"perms"+perm;
			var toString:String = MD5.hash(fromString);
			return toString;
		}
		
		/**
		 * Return the buddyicon image path
		 */
		public static function getBuddyIcon(user:User):String
		{
			if(user.iconServer > 0)
			{
				return Utils.static_url + user.iconServer + "/buddyicons/" + user.nsid + ".jpg";
			} else {
				return "http://www.flickr.com/images/buddyicon.jpg";
			}
		}		
	}
}